package org.jiamandan.com.strategy.impl.car.listener;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jiamandan.com.constant.YxConstant;
import org.jiamandan.com.dto.YXBasicDto;
import org.jiamandan.com.service.IRechargePileService;
import org.jiamandan.com.strategy.impl.car.IYXContextListener;
import org.jiamandan.com.utils.CommonUtil;
import org.jiamandan.com.utils.YxObjectUtil;
import org.springframework.stereotype.Service;

/**
 * @version Revision 1.0.1
 * @版权：版权所有 (c) 2021
 * @author:Lee
 * @email:honvy.lee@qq.com
 * @see:
 * @创建日期：2023-03-23 15:13
 * @功能说明：
 * @begin
 * @修改记录:
 * @修改后版本 修改人        修改内容
 * @2023-03-23 15:13	         Lee        	创建
 * @end
 */
@Service(value = YxConstant.Service.CMD_5)
@AllArgsConstructor
@Slf4j
public class YxCalculateModeVerfyListener implements IYXContextListener {

    private static final int[] number = {14, 4};
    private IRechargePileService iRechargePileService;

    @Override
    public void doExecute(YXBasicDto dto, Channel client) throws Exception {
        String pileNo = StringUtils.substring(dto.getData(), 0, number[0]);
        String modeNo = StringUtils.substring(dto.getData(), number[0], number[0] + number[1]);

        StringBuilder builder = new StringBuilder();
        builder.append(pileNo);
        builder.append(modeNo);
        builder.append("01");
        String replyMsg = YxObjectUtil.replyMessage(builder.toString(), dto.getSequence(), YxConstant.Cmd.CMD_6);
        log.info("JMD-YX计费模型验证请求应答(CMD = 06)：" + replyMsg);
        ByteBuf buf = Unpooled.buffer(replyMsg.getBytes().length);
        buf.writeBytes(CommonUtil.hexToByteArray(replyMsg));
        ChannelFuture future = client.writeAndFlush(buf);
    }
}
